package edu.zju.gis.dldsj.service.impl;

import edu.zju.gis.dldsj.dao.UserModelMapper;
import edu.zju.gis.dldsj.entity.UserModel;
import edu.zju.gis.dldsj.service.UserModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yanlo yanlong_lee@qq.com
 * @version 1.0 2018/08/09
 */
@Service
public class UserModelServiceImpl implements UserModelService {
    @Autowired
    private UserModelMapper userModelMapper;

    @Override
    public UserModel select(String pk) {
        return userModelMapper.selectByPrimaryKey(pk);
    }

    @Override
    public int insert(UserModel userModel) {
        return userModelMapper.insertSelective(userModel);
    }

    @Override
    public void update(UserModel userModel) {
        userModelMapper.updateByPrimaryKeySelective(userModel);
    }

    @Override
    public void delete(String s) {
        userModelMapper.deleteByPrimaryKey(s);
    }

    @Override
    public boolean isExist(String s) {
        return userModelMapper.selectByPrimaryKey(s) != null;
    }

    @Override
    public List<UserModel> getByPage(int offset, int size) {
        return userModelMapper.selectByPage(offset, size);
    }

    @Override
    public List<UserModel> get(String userId) {
        return userModelMapper.getByUserId(userId);
    }

    @Override
    public UserModel get(String userId, String modelId) {
        return userModelMapper.selectByUserModel(userId, modelId);
    }

    @Override
    public void insert(List<UserModel> userModels) {
        userModels.forEach(userModelMapper::insertSelective);
    }

    @Override
    public void upsert(UserModel userModel) {
        UserModel old = userModelMapper.selectByUserModel(userModel.getUserId(), userModel.getModelId());
        if (old != null) {
            userModelMapper.deleteByPrimaryKey(old.getId());
        }
        userModelMapper.insertSelective(userModel);
    }

    @Override
    public void updateStatus(String artifactId, String status) {
        userModelMapper.updateStatus(artifactId, status);
    }

    @Override
    public void delete(String userId, String modelId) {
        userModelMapper.deleteByUserModel(userId, modelId);
    }
}
